﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using POSPlus.QueryCreator;
using System.Data.SqlClient;
using Seagull.BarTender.Print;
using System.Configuration;
using System.Threading;

namespace POSPlus.EZBarTender
{
    /// <summary>
    /// Organizes and delegates the printing operations.
    /// </summary>
    public partial class PrinterController : Form
    {
        private BatchPrinter _instanceRef;

        public const int Regular = 1;
        public const int SuperSave = 2;
        public const int SpecialDeal = 4;
        public const int NowEvenLower = 8;
        public const int SmartBuy = 16;
        public const int BonusBucks = 32;

        public PrinterController()
        {
            InitializeComponent();
            initializeTags();
			/*
            _regularComponents = new List<PrintJobComponent>();
            _superSaveComponents = new List<PrintJobComponent>();
            _specialDealComponents = new List<PrintJobComponent>();
            _nelComponents = new List<PrintJobComponent>();
            _smartBuyComponents = new List<PrintJobComponent>();
            _bonusBucksComponents = new List<PrintJobComponent>();
            _regularDiscoComponents = new List<PrintJobComponent>();
			 */
        }
        public PrinterController(BatchPrinter instanceReference)
        {
            InitializeComponent();
            initializeTags();
            Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, Screen.PrimaryScreen.Bounds.Height / 2 - Height / 2);
			/*
            _regularComponents = new List<PrintJobComponent>();
            _superSaveComponents = new List<PrintJobComponent>();
            _specialDealComponents = new List<PrintJobComponent>();
            _nelComponents = new List<PrintJobComponent>();
            _smartBuyComponents = new List<PrintJobComponent>();
            _bonusBucksComponents = new List<PrintJobComponent>();
            _regularDiscoComponents = new List<PrintJobComponent>();
			*/
            _instanceRef = instanceReference;
        }
        
        public void AddPrintComponent(string description, string key, TagType type, int numberItems)
        {
            switch (type)
            {
                case TagType.Regular:
 //                   _regularComponents.Add(new PrintJobComponent(key, description));
                    ListViewItem item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 18.0).ToString()});
                    item.Group = _listView.Groups["_listGroupRegular"];
                    _listView.Items.Add(item);
                    break;
                case TagType.SuperSale:
 //                   _superSaveComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 6.0).ToString()});
                    item.Group = _listView.Groups["_listGroupSuperSale"];
                    _listView.Items.Add(item);
                    break;
                case TagType.SpecialDeal:
 //                   _specialDealComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 6.0).ToString()});
                    item.Group = _listView.Groups["_listGroupSpecialDeal"];
                    _listView.Items.Add(item);
                    break;
                case TagType.NowEvenLower:
  //                  _specialDealComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 6.0).ToString()});
                    item.Group = _listView.Groups["_listGroupNEL"];
                    _listView.Items.Add(item);
                    break;
                case TagType.SmartBuy:
 //                   _smartBuyComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 6.0).ToString()});
                    item.Group = _listView.Groups["_listGroupSmartBuy"];
                    _listView.Items.Add(item);
                    break;
                case TagType.BonusBucks:
 //                   _bonusBucksComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 6.0).ToString()});
                    item.Group = _listView.Groups["_listGroupBonusBucks"];
                    _listView.Items.Add(item);
                    break;
                case TagType.RegularDiscontinued:
 //                   _regularDiscoComponents.Add(new PrintJobComponent(key, description));
                    item = new ListViewItem(
                        new String[] {
                            description,
                            numberItems.ToString(),
                            Math.Ceiling(numberItems / 18.0).ToString()});
                    item.Group = _listView.Groups["_listGroupDiscoRegular"];
                    _listView.Items.Add(item);
                    break;
            }
                    
        }
		/*
        public void AddPrintComponent(PrintJobComponent component, int type)
        {
            switch (type)
            {
                case Regular:
                    _regularComponents.Add(component);
                    break;
                case SuperSave:
                    _superSaveComponents.Add(component);
                    break;
                case SpecialDeal:
                    _specialDealComponents.Add(component);
                    break;
                case NowEvenLower:
                    _nelComponents.Add(component);
                    break;
                case SmartBuy:
                    _smartBuyComponents.Add(component);
                    break;
                case BonusBucks:
                    _bonusBucksComponents.Add(component);
                    break;
            }
        }
		 */
		public void AddPrintComponent(Query query, String description, TagType type)
		{
			switch (type)
			{
				case TagType.Regular:
					AddPrintComponent(query, description,/* _regularComponents,*/ type, 18);
					break;
				case TagType.SuperSale:
					AddPrintComponent(query, description,/* _superSaveComponents,*/ type, 6);
					break;
				case TagType.SpecialDeal:
					AddPrintComponent(query, description,/* _specialDealComponents,*/ type, 6);
					break;
				case TagType.NowEvenLower:
					AddPrintComponent(query, description,/* _nelComponents,*/ type, 6);
					break;
				case TagType.RegularDiscontinued:
					AddPrintComponent(query, description,/* _regularDiscoComponents,*/ type, 18);
					break;
				case TagType.SmartBuy:
					AddPrintComponent(query, description,/* _smartBuyComponents,*/ type, 6);
					break;
				case TagType.BonusBucks:
					AddPrintComponent(query, description,/* _bonusBucksComponents,*/ type, 6);
					break;
			}
		}
		/*
        public void AddPrintComponent(From from, Where key, string description, TagType type)
        {
            switch (type)
            {
                case TagType.Regular:
                    AddPrintComponent(from, key, description, _regularComponents, type, 18);
                    break;
                case TagType.SuperSale:
                    AddPrintComponent(from, key, description, _superSaveComponents, type, 6);
                    break;
                case TagType.SpecialDeal:
                    AddPrintComponent(from, key, description, _specialDealComponents, type, 6);
                    break;
                case TagType.NowEvenLower:
                    AddPrintComponent(from, key, description, _nelComponents, type, 6);
                    break;
                case TagType.RegularDiscontinued:
                    AddPrintComponent(from, key, description, _regularDiscoComponents, type, 18);
                    break;
                case TagType.SmartBuy:
                    AddPrintComponent(from, key, description, _smartBuyComponents, type, 6);
                    break;
                case TagType.BonusBucks:
                    AddPrintComponent(from, key, description, _bonusBucksComponents, type, 6);
                    break;
            }
        }       
		 */
        public void AddPrintComponent(From from, Where key, string description, TagType type, int itemCount)
        {
            switch (type)
            {
                case TagType.Regular:
                    AddPrintComponent(from, key, description,/* _regularComponents, */type, itemCount, 18);
                    break;
                case TagType.SuperSale:
                    AddPrintComponent(from, key, description, /*_superSaveComponents, */type, itemCount, 6);
                    break;
                case TagType.SpecialDeal:
                    AddPrintComponent(from, key, description,/* _specialDealComponents,*/ type, itemCount, 6);
                    break;
                case TagType.NowEvenLower:
                    AddPrintComponent(from, key, description,/* _nelComponents, */type, itemCount, 6);
                    break;
                case TagType.SmartBuy:
                    AddPrintComponent(from, key, description,/* _smartBuyComponents,*/ type, itemCount, 6);
                    break;
                case TagType.BonusBucks:
                    AddPrintComponent(from, key, description, /*_bonusBucksComponents,*/ type, itemCount, 6);
                    break;
                case TagType.RegularDiscontinued:
                    AddPrintComponent(from, key, description, /*_regularDiscoComponents, */type, itemCount, 18);
                    break;
            } 
        }
        public void AddPrintComponent(From from, Where where, String description,/* List<PrintJobComponent> components,*/ TagType type, int itemCount, double divisor)
        {
            if (itemCount > 0)
            {
//                components.Add(new PrintJobComponent(where.ToString(), description));
                string key = "";
                switch (type)
                {
                    case TagType.Regular: key = "_listGroupRegular"; break;
                    case TagType.SuperSale: key = "_listGroupSuperSale"; break;
                    case TagType.SpecialDeal: key = "_listGroupSpecialDeal"; break;
                    case TagType.SmartBuy: key = "_listGroupSmartBuy"; break;
                    case TagType.NowEvenLower: key = "_listGroupNEL"; break;
                    case TagType.BonusBucks: key = "_listGroupBonusBucks"; break;
                    case TagType.RegularDiscontinued: key = "_listGroupDiscoRegular"; break;
                }
                ListViewItem newItem = new ListViewItem(new String[] {
                    description,
                    itemCount.ToString(),
                    Math.Ceiling(itemCount / divisor).ToString()});

                newItem.Group = _listView.Groups[key];
                _listView.Items.Add(newItem);
            }
        }

        public void AddPrintComponent(From from, Where where, string description,/* List<PrintJobComponent> components,*/ ListView listView, int itemCount, double divisor)
        {
            if (itemCount > 0)
            {
 //               components.Add(new PrintJobComponent(where.ToString(), description));
                listView.Items.Add(new ListViewItem(new string[] { description, itemCount.ToString(), Math.Ceiling(itemCount / divisor).ToString() }));
            }
            
        }

		public void AddPrintComponent(Query query, String description,/* List<PrintJobComponent> components,*/ TagType type, double divisor)
		{
			int itemCount = -1;

			Query newQuery = new Query(new Select("COUNT(*) AS ItemCount"), query.FROM, query.WHERE);

			Console.WriteLine("################################# AddPrintComponent #######################################");
			Console.WriteLine(newQuery.ToString());
			Console.WriteLine("################################# AddPrintComponent #######################################");

			SqlConnection sqlConnection = new SqlConnection(_instanceRef.ConnectionString);
			SqlCommand sqlCommand = new SqlCommand(newQuery.ToString(), sqlConnection);

			try
			{
				sqlConnection.Open();
				SqlDataReader reader;
				reader = sqlCommand.ExecuteReader();
				reader.Read();
				itemCount = reader.GetInt32(0);
				reader.Close();
			}
			catch (SqlException ex)
			{
				MessageBox.Show(ex.Message);
			}
			finally
			{
				sqlConnection.Close();
				sqlConnection.Dispose();
			}

			if (itemCount > 0)
			{
//				components.Add(new PrintJobComponent(query.WHERE.ToString(), description));
				String groupName = "";
				switch (type)
				{
					case TagType.Regular: groupName = "_listGroupRegular"; break;
					case TagType.BonusBucks: groupName = "_listGroupBonusBucks"; break;
					case TagType.NowEvenLower: groupName = "_listGroupNEL"; break;
					case TagType.SmartBuy: groupName = "_listGroupSmartBuy"; break;
					case TagType.SpecialDeal: groupName = "_listGroupSpecialDeal"; break;
					case TagType.SuperSale: groupName = "_listGroupSuperSale"; break;
					case TagType.RegularDiscontinued: groupName = "_listGroupDiscoRegular"; break;
				}
				ListViewItem newItem = new ListViewItem(
					new String[] {
                        description,
                        itemCount.ToString(),
                        Math.Ceiling(itemCount/divisor).ToString()});
				newItem.Group = _listView.Groups[groupName];
				_listView.Items.Add(newItem);
			}
		}
		/*
        public void AddPrintComponent(From from, Where where, string description, List<PrintJobComponent> components, TagType type, double divisor)
        {
            int itemCount = -1;

            Query query = new Query(
                new Select("COUNT(*)"),
                from,
                where);

            SqlConnection sqlConnection = new SqlConnection(_instanceRef.ConnectionString);
            SqlCommand sqlCommand = new SqlCommand(query.ToString(), sqlConnection); 

            try
            {
                sqlConnection.Open();
                SqlDataReader reader;
                reader = sqlCommand.ExecuteReader();
                reader.Read();
                itemCount = reader.GetInt32(0);
                reader.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
            }

            if (itemCount > 0)
            {
                components.Add(new PrintJobComponent(where.ToString(), description));
                String groupName = "";
                switch (type)
                {
                    case TagType.Regular: groupName = "_listGroupRegular"; break;
                    case TagType.BonusBucks: groupName = "_listGroupBonusBucks"; break;
                    case TagType.NowEvenLower: groupName = "_listGroupNEL"; break;
                    case TagType.SmartBuy: groupName = "_listGroupSmartBuy"; break;
                    case TagType.SpecialDeal: groupName = "_listGroupSpecialDeal"; break;
                    case TagType.SuperSale: groupName = "_listGroupSuperSale"; break;
                    case TagType.RegularDiscontinued: groupName = "_listGroupDiscoRegular"; break;
                }
                ListViewItem newItem = new ListViewItem(
                    new String[] {
                        description,
                        itemCount.ToString(),
                        Math.Ceiling(itemCount/divisor).ToString()});
                newItem.Group = _listView.Groups[groupName];
                _listView.Items.Add(newItem);
                
            }
        }
        */
		/*
        public Query GetQuery(TagType type, Select select, From from, OrderBy orderby)
        {
            switch (type)
            {
                case TagType.Regular:
                    return GetQuery(_regularComponents, select, from, orderby);
                case TagType.SuperSale:
                    return GetQuery(_superSaveComponents, select, from, orderby);
                case TagType.SpecialDeal:
                    return GetQuery(_specialDealComponents, select, from, orderby);
                case TagType.NowEvenLower:
                    return GetQuery(_nelComponents, select, from, orderby);
                case TagType.SmartBuy:
                    return GetQuery(_smartBuyComponents, select, from, orderby);
                case TagType.BonusBucks:
                    return GetQuery(_bonusBucksComponents, select, from, orderby);
                default:
                    return null;
            }
        }

        public Query GetQuery(List<PrintJobComponent> components, Select select, From from, OrderBy orderby)
        {
            _select = select;
            _from = from;
            _orderby = orderby;
            _where = new Where();

            foreach (PrintJobComponent component in components)
            {
                _where.AddExpression(component.KeyExpression, "OR");
            }

            _query = new Query(
                _select,
                _from,
                _where,
                _orderby);

            return _query;
        }
		*/
        private void _btnRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in _listView.SelectedItems)
            {
                string query = TableOperation.Delete(_instanceRef.BarTenderTableName,
                    " JobName = '" + item.Text + "'");

                _instanceRef.AddQueue(new SqlCommand(query, new SqlConnection(_instanceRef.ConnectionString)), null);

                _listView.Items.Remove(item);
            }
        }
		/*
        private List<PrintJobComponent> GetComponentFor(TagType selected)
        {
            switch (selected)
            {
                case TagType.Regular: return _regularComponents;
                case TagType.SuperSale: return _superSaveComponents;
                case TagType.SpecialDeal: return _specialDealComponents;
                case TagType.NowEvenLower: return _nelComponents;
                case TagType.BonusBucks: return _bonusBucksComponents;
                case TagType.SmartBuy: return _smartBuyComponents;
            }
            return null;
        }
        */
        private void _btnPrintAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewGroup group in _listView.Groups)
            {
                if (group.Items.Count > 0)
                {
                    Query query = new Query(new Select("*"), new From(_instanceRef.BarTenderTableName));
                    query.AddOrderByField("ISNULL(Rehang, -1)");
                    query.AddOrderByField("ISNULL(LocAisle, 9999999)");
                    query.AddOrderByField("LocBlock");
                    query.AddOrderByField("LocSeq");

                    foreach (ListViewItem item in group.Items)
                    {
                        query.AddWhereExpression("JobName = '" + item.Text + "'", "OR");
                    }

                    BarTenderTreeBuilder builder = new BarTenderTreeBuilder(
                        ConfigurationManager.AppSettings["XMLDocumentLocation"], true);
                    string previewFormatLocation = "";
                    switch ((TagType)group.Tag)
                    {
                        case TagType.Regular: previewFormatLocation = "RegularLabelFormatLocation"; break;
                        case TagType.SpecialDeal: previewFormatLocation = "SpecialLabelFormatLocation"; break;
                        case TagType.SuperSale: previewFormatLocation = "SuperSaleLabelFormatLocation"; break;
                        case TagType.RegularDiscontinued: previewFormatLocation = "RegularDiscoLabelFormatLocation"; break;
                        case TagType.NowEvenLower: previewFormatLocation = "SignDiscoLabelFormatLocation"; break;
                    }

                    builder.SetFormatLocation(ConfigurationManager.AppSettings[previewFormatLocation]);
                    builder.JobName = group.Header;

                    PrintPreview printPreview = new PrintPreview(ConfigurationManager.AppSettings["XMLDocumentLocation"]);

                    DialogResult result = printPreview.ShowDialog();

                    // If print chosen then print
                    if (result == DialogResult.OK)
                    {
                        SqlConnection sqlConnection = new SqlConnection(_instanceRef.ConnectionString);
                        SqlDataAdapter adapter = new SqlDataAdapter(query.ToString(), sqlConnection);
                        DataTable table = new DataTable();
                        adapter.Fill(table);

                        // Select the Correct Normalizer.
                        LabelNormalizer normalizer = null;
                        String formatLocation = "";

                        switch ((TagType)group.Tag)
                        {
                            case TagType.Regular:
                                normalizer = new RegularNormalizer();
                                formatLocation = "RegularLabelFormatLocation";
                                break;
                            case TagType.BonusBucks:
                            case TagType.SpecialDeal:
                            case TagType.SuperSale:
                                normalizer = new SpecialNormalizer();
                                formatLocation = "SpecialLabelFormatLocation";
                                break;
                            case TagType.RegularDiscontinued:
                                normalizer = new RegularNormalizer();
                                formatLocation = "RegularDiscoLabelFormatLocation";
                                break;
                            case TagType.NowEvenLower:
                                normalizer = new SpecialNormalizer();
                                formatLocation = "SignDiscoLabelFormatLocation";
                                break;
                            default: Console.WriteLine("Error: Non-Valid Normalizer Chosen.");
                                return;
                        }
                        normalizer.Normalize(table);

                        // Create the Builder for the table
                        builder = new BarTenderTreeBuilder(ConfigurationManager.AppSettings["XMLDocumentLocation"],
                            "");
                        builder.SetFormatLocation(ConfigurationManager.AppSettings[formatLocation]);
                        builder.JobName = group.Header;
                        builder.FillTextData(normalizer.LabelInformation);
                        builder.ClipLastNewLine();
                        builder.CloseTextData();

                        // Run the background worker.
                        this.Enabled = false;
                        _instanceRef.Enabled = false;
                        _backgroundWorker.RunWorkerAsync();

                        // Wait a few seconds.
                        Thread.Sleep(2000);

                        // Bring up in between dialog asking to continue
                        DialogResult cont = MessageBox.Show(
                            "After the job has finished click 'OK' to continue or 'Cancel' to not print next job.",
                            "Print Finished",
                            MessageBoxButtons.OKCancel);

                        if (cont == System.Windows.Forms.DialogResult.Cancel)
                            return;
                    }
                }
            }
        }

        private void _btnPrintSelected_Click(object sender, EventArgs e)
        {
            // Determine the type of tag to print.
            foreach (ListViewGroup group in _listView.Groups)
            {
				int selectedItemsInGroup = 0;
				foreach (ListViewItem item in group.Items)
				{
					if (item.Selected)
						selectedItemsInGroup++;
				}

                if (selectedItemsInGroup > 0)
                {
                    Query query = new Query(new Select("*"), new From(_instanceRef.BarTenderTableName));
                    query.AddOrderByField("ISNULL(Rehang, -1)");
                    query.AddOrderByField("ISNULL(LocAisle, 9999999)");
                    query.AddOrderByField("LocBlock");
                    query.AddOrderByField("LocSeq");

                    foreach (ListViewItem item in group.Items)
                    {
                        if (item.Selected)
                        {
                            // Add to the query
                            query.AddWhereExpression("JobName = '" + item.Text + "'", "OR");
                        }

                    }

                    // Create the print preview for the item
                    BarTenderTreeBuilder builder = new BarTenderTreeBuilder(
                        ConfigurationManager.AppSettings["XMLDocumentLocation"],
                        true);

                    string previewFormatLocation = "";
                    switch ((TagType)group.Tag)
                    {
                        case TagType.Regular: previewFormatLocation = "RegularLabelFormatLocation"; break;
                        case TagType.SpecialDeal: previewFormatLocation = "SpecialLabelFormatLocation"; break;
                        case TagType.SuperSale: previewFormatLocation = "SuperSaleLabelFormatLocation"; break;
                        case TagType.RegularDiscontinued: previewFormatLocation = "RegularDiscoLabelFormatLocation"; break;
                        case TagType.NowEvenLower: previewFormatLocation = "SignDiscoLabelFormatLocation"; break;
                    }


                    builder.SetFormatLocation(ConfigurationManager.AppSettings[previewFormatLocation]);
                    builder.JobName = group.Header;

                    PrintPreview preview = new PrintPreview(ConfigurationManager.AppSettings["XMLDocumentLocation"]);

                    // Bring up the print preview dialog
                    DialogResult result = preview.ShowDialog();

                    // If print chosen then print
                    if (result == DialogResult.OK)
                    {
                        SqlConnection sqlConnection = new SqlConnection(_instanceRef.ConnectionString);
                        SqlDataAdapter adapter = new SqlDataAdapter(query.ToString(), sqlConnection);
                        DataTable table = new DataTable();
                        adapter.Fill(table);

                        // Select the Correct Normalizer.
                        LabelNormalizer normalizer = null;
                        String formatLocation = "";

                        switch ((TagType)group.Tag)
                        {
                            case TagType.Regular:
                                normalizer = new RegularNormalizer();
                                formatLocation = "RegularLabelFormatLocation";
                                break;
                            case TagType.BonusBucks:
                            case TagType.SpecialDeal:
                            case TagType.SuperSale:
                                normalizer = new SpecialNormalizer();
                                formatLocation = "SpecialLabelFormatLocation";
                                break;
                            case TagType.RegularDiscontinued:
                                normalizer = new RegularNormalizer();
                                formatLocation = "RegularDiscoLabelFormatLocation";
                                break;
                            case TagType.NowEvenLower:
                                normalizer = new SpecialNormalizer();
                                formatLocation = "SignDiscoLabelFormatLocation";
                                break;
                            default: Console.WriteLine("Error: Non-Valid Normalizer Chosen.");
                                return;
                        }
                        normalizer.Normalize(table);

                        // Create the Builder for the table
                        builder = new BarTenderTreeBuilder(ConfigurationManager.AppSettings["XMLDocumentLocation"],
                            "");
                        builder.SetFormatLocation(ConfigurationManager.AppSettings[formatLocation]);
                        builder.JobName = group.Header;
                        builder.FillTextData(normalizer.LabelInformation);
                        builder.ClipLastNewLine();
                        builder.CloseTextData();

                        // Run the background worker.
                        this.Enabled = false;
                        _instanceRef.Enabled = false;
                        _backgroundWorker.RunWorkerAsync();

                        // Wait a few seconds.
                        Thread.Sleep(2000);

                        // Bring up in between dialog asking to continue
                        DialogResult cont = MessageBox.Show(
                            "After the job has finished click 'OK' to continue or 'Cancel' to not print next job.",
                            "Print Finished",
                            MessageBoxButtons.OKCancel);

                        if (cont == System.Windows.Forms.DialogResult.Cancel)
                            return;
                    }
                }
            }
        }

        private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Engine engine = new Engine(true);
            Messages messages = null;
            engine.XMLScript(ConfigurationManager.AppSettings["XMLDocumentLocation"], XMLSourceType.ScriptFile, out messages);
            foreach (Seagull.BarTender.Print.Message msg in messages)
            {
                MessageBox.Show(msg.Text);
            }
            engine.Stop();           
        }

        private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Enabled = true;
            _instanceRef.Enabled = true;
        }

        private void initializeTags()
        {
            _listView.Groups["_listGroupRegular"].Tag = TagType.Regular;
            _listView.Groups["_listGroupSuperSale"].Tag = TagType.SuperSale;
            _listView.Groups["_listGroupSpecialDeal"].Tag = TagType.SpecialDeal;
            _listView.Groups["_listGroupNEL"].Tag = TagType.NowEvenLower;
            _listView.Groups["_listGroupBonusBucks"].Tag = TagType.BonusBucks;
            _listView.Groups["_listGroupSmartBuy"].Tag = TagType.SmartBuy;
            _listView.Groups["_listGroupDiscoRegular"].Tag = TagType.RegularDiscontinued;
        }
    }
}